﻿using System;

using DreamCube.Foundation.Basic.Utility;
using DreamCube.Foundation.Basic.Objects;

namespace DreamCube.Foundation.WMI
{
    [AttributeUsage(AttributeTargets.Property)]
    public class ManagementObjectPropertyMappingAttribute : Attribute
    {
        #region "属性"

        /// <summary>
        /// 关联的ManagementObject对象的属性名
        /// </summary>
        public String ManagementObjectPropertyName
        {
            get;
            set;
        }

        /// <summary>
        /// 转换数据的委托方法
        /// </summary>
        public Func<Object, Object> ConvertDataFun
        {
            get;
            private set;
        }

        #endregion

        public ManagementObjectPropertyMappingAttribute(String managementObjectPropertyName)
            : this(managementObjectPropertyName, null)
        { }

        /// <summary>
        /// 指定转换格式的对象类型
        /// </summary>
        /// <param name="managementObjectPropertyName"></param>
        /// <param name="type">指定转换格式的对象类型</param>
        public ManagementObjectPropertyMappingAttribute(String managementObjectPropertyName, Type type)
        {
            this.ManagementObjectPropertyName = managementObjectPropertyName;
            if (type != null)
            {
                IDataConvert<FileSystemSize> converter = MyType.CreateInstance<IDataConvert<FileSystemSize>>(type);
                if (converter != null)
                {
                    ConvertDataFun = new Func<Object, Object>(
                        (o) =>
                        {
                            return converter.Convert(o);
                        });
                }
            }
            else ConvertDataFun = null;
        }
    }
}
